package com.webside.front.rcomment.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.webside.front.common.model.RComment;
import com.webside.front.common.service.RCommentService;
import com.webside.util.SensitivewordFilter;

/**
 * 该控制器负责插入回复和点赞
 * 
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping(value = "/comment/")
public class RCommentController {
	@Autowired
	RCommentService rCommentService;
	
	/**
	 * 查询父评论
	 * @param m
	 * @param seriesId
	 * @param videoId
	 * @param channel
	 * @return
	 */
	@RequestMapping(value="getParentComments",method={RequestMethod.POST})
	public @ResponseBody Object getParentComments(Integer seriesId, Integer videoId, Integer videoFromWebId, Integer pageNo){
		Map<String,Object> condition = new HashMap<String,Object>();
		//
		if (seriesId!=null) {
			condition.put("seriesId", seriesId);
		}
		if (videoId!=null) {
			condition.put("videoId", videoId);
		}
		if(videoFromWebId!=null){
			condition.put("videoFromWebId", videoFromWebId);
		}
		System.out.println("传入的页码是:"+pageNo);
		//未删除状态
		condition.put("status", "1");
		//类型是1
		condition.put("type", "2");
		//排序规则
		condition.put("orderColumn", "create_time");
		condition.put("orderType", "DESC");
		//每页大小
		condition.put("pageSize", 5);
		Integer pgNo = pageNo * 5;
		//从第几条开始
		condition.put("pageNo", pgNo);
		List<RComment> list= rCommentService.selectCommentsByCondition(condition);
		SensitivewordFilter filter=new SensitivewordFilter();
		for (int i = 0; i < list.size(); i++) {
			String content= filter.replaceSensitiveWord(list.get(i).getContent(), 1, "*");
			list.get(i).setContent(content);
		}
		System.out.println("长度是"+list.size());
		return list;
	}
	
	
	/**
	 * 查询子评论
	 * @param parentId
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value="getChildrenComments",method={RequestMethod.POST})
	public @ResponseBody Object getChildrenComment(Integer parentId,Integer pageNo){
		Map<String,Object> condition = new HashMap<String,Object>();
		System.out.println("传入的父评论id"+parentId);
		condition.put("parentId", parentId);
		//未删除状态
		condition.put("status", "1");
		//类型是2
		condition.put("type", "2");
		//排序规则
		condition.put("orderColumn", "create_time");
		condition.put("orderType", "DESC");
		//每页大小
		condition.put("pageSize", 3);
		Integer pgNo = pageNo * 3;
		//从第几条开始
		condition.put("pageNo", pgNo);
		
		List<RComment> list= rCommentService.selectCommentsByCondition(condition);
		SensitivewordFilter filter=new SensitivewordFilter();
		for (int i = 0; i < list.size(); i++) {
			String content= filter.replaceSensitiveWord(list.get(i).getContent(), 1, "*");
			list.get(i).setContent(content);
		}
		
		if(list.size()>0){
			return list;
		}else{
			if(pageNo!=0){
				return "加载完毕";
			}else{
				return "尚未回复评论";
			}
		}
	}
	
	
	public int getCount(Map<String, Object> condition){
		return rCommentService.selectCountByCondition(condition);
	}
	
	/**
	 *  新增评论或点赞
	 * @param type
	 * @param videoId
	 * @param parentId
	 * @param content
	 * @param childrenCount
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="insertComment",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object insertComment(String type,Integer videoFromWebId, Integer videoId,Integer parentId,String content, String childrenCount,String ipCode) throws Exception{
		boolean a = false;
		System.out.println(type+videoId+parentId+content+childrenCount);
		String c = null;
		try {
			c = URLDecoder.decode(content,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println(c);
		RComment comment=new RComment();
		comment.setVideoId(videoId);
		System.out.println(ipCode);
		comment.setIpcode(ipCode);
		comment.setVideoFromWebId(videoFromWebId);
		if (parentId==null || "".equals(parentId)) {
			comment.setParentId(0);
		}else{
			comment.setParentId(parentId);
			
		}
		comment.setContent(c);
		comment.setType(type);
		
		System.out.println("现在传入的type是："+type);
		comment.setStatus("1");
		Date date=new Date();
		if ("2".equals(type)) {
			if(childrenCount.equals("0") && childrenCount!=null){
				comment.setChildrenCount(1);
			}
			comment.setCreateTime(date);
			a = rCommentService.insertCommentOrLike(comment);
			if (parentId!=0) {
				int flag = rCommentService.updateChildrenCount(parentId);
				if (flag>0) {
					System.out.println("更新子回复数量成功");
				}else{
					throw new Exception("更新子回复数量失败");
				}
			}
			System.out.println("回复+1");
			if (videoId!=null) {
				System.out.println("回复条数被插入啦！");
			}
			
		}else if("1".equals(type)){
			comment.setCreateTime(date);
			a = rCommentService.insertCommentOrLike(comment);
			System.out.println("点赞+1");
			if (videoId!=null) {
				System.out.println("点赞条数被更新啦！");
			}
		}
		if (a) {
			System.out.println("插入纪录成功");
			return 1;
		}else
			//新增失败，返回0
			return 0;
	}
	@RequestMapping(value="pageCount",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody int getPageCount(Integer videoFromWebId){
		Map<String, Object> condition = new HashMap<String,Object>();
		condition.put("status", "1");
		condition.put("type", "2");
		condition.put("videoFromWebId", videoFromWebId);
		
		int count = rCommentService.selectCountByCondition(condition);
		
		int pageCount = (int)Math.ceil((double)count/5);
		return pageCount;
	}
	
}
